package Pocker;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Random;

public class Game {
    public static void main(String[] args) {
        //1.初始化一副牌
        ArrayList<Card> cardList=initCardList();
        //2.洗牌
        shuffle(cardList);
        //Collections.shuffle(cardList);  该类中有洗牌算法

        //查找某个牌是否存在
//        Card spadeA=new Card(1,"♠");
//        System.out.println(cardList.contains(spadeA));

        //3.准备三个玩家的牌（存在牌的容器，通过ArrayList<Card>来表示）
        ArrayList<Card> playerA=new ArrayList<>();
        ArrayList<Card> playerB=new ArrayList<>();
        ArrayList<Card> playerC=new ArrayList<>();

        System.out.print("抓牌前：");
        System.out.println(cardList);


        //4.三个人按照顺序依次抓牌，每个人一次抓一张，共抓五张
        for(int i=0;i<5;i++) {
            Card card=cardList.remove(0);
            playerA.add(card);

            card=cardList.remove(0);
            playerB.add(card);

            card=cardList.remove(0);
            playerC.add(card);
        }

        System.out.print("抓牌后：");
        System.out.println(cardList.size());
        System.out.println("A:"+playerA);
        System.out.println("B:"+playerB);
        System.out.println("C:"+playerC);




    }

    private static void shuffle(ArrayList<Card> cardList) {//洗牌
        Random random=new Random();

        for(int i=cardList.size()-1;i>0;i--){
            //从最后一个依次向前 洗牌
            //1.随机获得需要交换 另一张牌的下标，random.nextInt(i)该方法随机获得数在[0,i)之间
            int randomIndex=random.nextInt(i);

            Card tmpH=cardList.get(i);
            Card tmpQ=cardList.get(randomIndex);
            cardList.set(i,tmpQ);
            cardList.set(randomIndex,tmpH);

        }
    }

    private static ArrayList initCardList() {//初始化牌
        ArrayList cardList=new ArrayList();
        String[] suits={"♥","♠","♣","♦"};

        for(String suit:suits) {//遍历每一种花色
            for(int rank=1;rank<=13;rank++) {
                Card card=new Card(rank,suit);
                cardList.add(card);
            }

        }
        return cardList;
    }
}
